<template>
	<el-dialog title="添加橱窗商品" :modal-append-to-body="false" :visible.sync="isShow" :close-on-click-modal="false" :before-close="close" width="1200px">
		<div class="form-box">
			<el-form ref="form" :model="form" :rules="rules" label-width="110px">
				<el-row>
					<el-col :span="24">
						<el-form-item label="选择展示位置" prop="moduleNo">
							<el-radio-group v-model="form.moduleNo" @change="chooseModule">
								<el-radio label="index-platform">首页平台推荐模块</el-radio>
								<el-radio label="index-shop">首页店铺(自营)推荐模块</el-radio>
								<el-radio label="index-bottom">首页底部橱窗</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</div>
		
		<template v-if="form.moduleNo">
			<!-- 搜索商品 -->
			<div class="search-box">
				<el-form ref="searchForm" class="search-form" :inline="true" :model="searchForm" label-width="90px">
					<el-form-item label="选择店铺" prop="shopId" v-if="form.moduleNo != 'index-platform'">
						<el-select v-model="searchForm.shopId" size="small" filterable placeholder="请输入名字或下拉选择" style="width: 250px;">
							<el-option :label="item.shopName" :value="item.id" v-for="item of allShopList" :key="item.id"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="商品名称" prop="productName">
						<el-input type="text" size="small" v-model="searchForm.productName" placeholder="请输入商品名称" style="width: 250px;"></el-input>
					</el-form-item>
					<el-form-item>
						<div class="search-submit">
							<el-button type="primary" size="small" @click="submitSearch">查询</el-button>
							<el-button size="small" @click="resetSearch">重置</el-button>
						</div>
					</el-form-item>
				</el-form>
			</div>
			
			<!-- 商品列表表格 -->
			<el-table :data="productList" stripe border style="width: 100%">
				<el-table-column type="index" fixed label="序号" width="50" align="center"></el-table-column>
				
				<el-table-column prop="skuThumbImage" label="缩略图" width="80" align="center">
					<template slot-scope="scope">
						<el-image style="width: 35px; height: 35px" :src="scope.row.skuThumbImage" :preview-src-list="[scope.row.skuThumbImage]"></el-image>
					</template>
				</el-table-column>
				<el-table-column prop="productName" label="商品名称" align="center" width="180"></el-table-column>
				<el-table-column prop="productCode" label="商品编码" align="center" width="120"></el-table-column>
				<el-table-column prop="skuName" label="SKU名称" align="center" width="80"></el-table-column>
				<el-table-column prop="skuCode" label="SKU编码" align="center" width="120"></el-table-column>
				<el-table-column prop="priceShow" label="原价(元)" align="center" width="90"></el-table-column>
				<el-table-column prop="price" label="销售价(元)" align="center" width="90"></el-table-column>
				
				<el-table-column prop="saleChannel" label="销售渠道" align="center" width="100">
					<template slot-scope="scope">
						<el-tag size="small" type="success" v-if="scope.row.saleChannel == 1">商家/供应商</el-tag>
						<el-tag size="small" type="warning" v-else-if="scope.row.saleChannel == 2">直播平台</el-tag>
						<el-tag size="small" v-else>微信小程序</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="状态" align="center">
					<template slot-scope="scope">
						<el-tag size="small" :type="scope.row.status === 1 ? 'success' : 'warning'" disable-transitions>{{scope.row.status==0 ? "下架" : "上架"}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="saleStatus" label="售卖状态" align="center">
					<template slot-scope="scope">
						<el-tag size="small" :type="scope.row.saleStatus === 1 ? 'success' : 'info'" disable-transitions>{{scope.row.saleStatus == 0 ? "已停售" : "在售中"}}</el-tag>
					</template>
				</el-table-column>
				
				<el-table-column label="操作" fixed="right" width="120">
					<template slot-scope="scope">
						<el-button type="info" size="mini" disabled v-if="scope.row.showcaseModuleStatus">已添加</el-button>
						<el-button type="primary" size="mini" @click="chooseProduct(scope.row)" v-else>点击选择</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="page">
				<el-pagination
					background
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
					:current-page="productListPage"
					:page-sizes="[10, 20, 50, 100]"
					:page-size="productListPageSize"
					layout="total, sizes, prev, pager, next, jumper"
					:total="productListTotal"
				></el-pagination>
			</div>
		</template>
		
		<div slot="footer">
			<!-- <el-button type="primary" @click="submit">保 存</el-button> -->
			<el-button @click="close">关 闭</el-button>
		</div>
	</el-dialog>
</template>

<script>
	import { getShopList } from '@api/shop/shop'
	import { findAllProductSkuSaleListByShop } from '@api/product/productSkuSale'
	import { addDisplayProduct } from '@api/platform/platformMarketing'
	export default {
		props: {
			isShow: Boolean,
			item: Object,
			isEdit: Boolean,
		},
		data() {
			return {
				form: {
					moduleNo: '',
					skuSaleId: '',
					skuSaleName: '',
				},
				rules: {
					moduleNo: [{ required: true, message: '请选择展示位置', trigger: 'blur' }],
					// skuSaleId: [{ required: true, message: '请选择商品', trigger: 'blur' }],
				},
				
				searchForm: {
					shopId: '',
					productName: '',
					productBarCode: '',
				},
				
				productList: [], //商品列表
				productListPage: 1,
				productListPageSize: 10,
				productListTotal: 0,
				
				allShopList: [], //店铺列表
			}
		},
		watch: {
			isShow(val) {
				if(val) {
					this.getAllShopData();
				}
			},
		},
		methods: {
			getAllShopData() { //获取全部店铺列表
				getShopList({
					shopPickupStatus: 1,
					pageNum: 1,
					pageSize: 9999
				})
					.then(res => {
						let list = res.data.content;
						let newList = [];
						list.forEach(item => {
							if(item.id != 0) {
								newList.push(item);
							}
						});
						
						this.allShopList = newList;
					})
			},
			
			searchShopProduct() { //搜索商品
				findAllProductSkuSaleListByShop({
					shopId: this.searchForm.shopId,
					productName: this.searchForm.productName,
					productBarCode: this.searchForm.productBarCode,
					moduleNo: this.form.moduleNo,
					status: 1,
					saleStatus: 1,
					saleChannel: 0,
					currentPage: this.productListPage,
					pageSize: this.productListPageSize
				})
					.then(res => {
						this.productList = res.data.content;
						this.productListTotal = res.data.total;
					})
			},
			handleSizeChange(val) {
				this.productListPageSize = val;
				this.searchShopProduct();
			},
			handleCurrentChange(val) {
				this.productListPage = val;
				this.searchShopProduct();
			},
			submitSearch() {
				this.productListPage = 1;
				this.searchShopProduct();
			},
			resetSearch() {
				this.searchForm.shopId = '';
				this.searchForm.productName = '';
				this.searchForm.productBarCode = '';
				this.productListPage = 1;
				this.searchShopProduct();
			},
			
			chooseModule(val) {
				if(val == 'index-platform') {
					this.searchForm.shopId = 0;
					this.searchShopProduct();
				} else {
					this.searchForm.shopId = '';
					this.searchForm.productName = '';
					this.searchForm.productBarCode = '';
					this.productListPage = 1;
					this.productList = [];
				}
			},
			chooseProduct(item) { //选择商品
				let params = {
					skuSaleId: item.id,
					moduleNo: this.form.moduleNo,
				}
				addDisplayProduct(params)
					.then(res => {
						item.showcaseModuleStatus = true;
						this.$message.success('商品添加成功！');
						this.$emit('submit');
					})
			},
			
			reset() {
				this.form.skuSaleId = '';
				this.form.skuSaleName = '';
				this.form.moduleNo = '';
				
				this.searchForm.shopId = '';
				this.searchForm.productName = '';
				this.searchForm.productBarCode = '';
				this.productListPage = 1;
				this.productListTotal = 0;
				this.productList = [];
			},
			close() {
				this.$emit('close');
				this.reset();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.search-box {
		background: #f4f4f4;
		padding: 8px;
		.search-form > div {
			width: auto;
		}
		.el-form-item {
			margin-bottom: 0;
		}
		.search-submit {
			height: 40px;
			align-items: center;
			display: flex;
			padding-left: 30px;
		}
	}
	.el-table {
		::v-deep .el-table__cell {
			padding: 5px 0;
		}
	}
	.page {
		margin-top: 10px;
	}
</style>